package com.outsourcingmanagesystem.service.impl;

import com.outsourcingmanagesystem.common.MessageConstant;
import com.outsourcingmanagesystem.common.exception.PasswordErrorException;
import com.outsourcingmanagesystem.entity.WorkHour;
import com.outsourcingmanagesystem.entity.dto.UserLoginDTO;
import com.outsourcingmanagesystem.entity.User;
import com.outsourcingmanagesystem.mapper.UserMapper;
import com.outsourcingmanagesystem.mapper.WorkHourMapper;
import com.outsourcingmanagesystem.service.UserService;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.outsourcingmanagesystem.common.exception.*;
import org.springframework.util.DigestUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private WorkHourMapper workHourMapper;
    public User login(UserLoginDTO UserLoginDTO) {
        String username = UserLoginDTO.getUsername();
        String password = UserLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        User user = userMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (user == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        // 对前端传过来的密码进行明文加密处理
        // 创建 BCrypt 加密器（强度默认 10）
//       BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
//        String encodedPassword = encoder.encode(password);
        password =  DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(user.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        //3、返回实体对象
        return user;
    }

    @Override
    public void register(UserLoginDTO userLoginDTO) {
        User user = new User();
        user.setUsername(userLoginDTO.getUsername());
        user.setPassword(DigestUtils.md5DigestAsHex(userLoginDTO.getPassword().getBytes()));
        user.setBaseSalary(BigDecimal.valueOf(3000.0));
        user.setPerformance(BigDecimal.valueOf(1.0));
        user.setStatus((byte) 0);
        userMapper.insert(user);
    }

    @Override
    public void updateStatus(User user) {
        WorkHour workHour = new WorkHour();
        workHour.setUserId(user.getUserId());
        workHour.setCreatedTime(LocalDateTime.now());
        user.setStatus((byte) 1);
        workHourMapper.insert(workHour);
        userMapper.updateById( user);
    }

    @Override
    public void show(User user) {
        userMapper.selectById(user.getUserId());
    }

    @Override
    public void offWork(User user) {
       WorkHour workHour = workHourMapper.selectById(user.getUserId());
       workHour.setEndTime(LocalDateTime.now());
       workHour.setHours(workHour.getEndTime().getHour()-workHour.getCreatedTime().getHour());
       workHourMapper.updateById(workHour);
        user.setStatus((byte) 0);
        userMapper.updateById(user);
    }

}
